package com.infosys.newSimpleInvertedIndex.engine;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import com.infosys.newSimpleInvertedIndex.models.Document;
import com.infosys.newSimpleInvertedIndex.models.ParsedDoc;
import com.infosys.newSimpleInvertedIndex.models.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.apache.commons.lang3.StringUtils;

public class DocumentParser {

    private ConcurrentHashMap<String, String> stringPool;

    public DocumentParser() {
        this.stringPool = new ConcurrentHashMap<>();
    }

    public ParsedDoc parseDocument(Document document){
        List<Term> terms = rawTextToTermList(document.getRawText());
        // return the parsed doc
        ParsedDoc parsedDoc = new ParsedDoc( terms, document.getId() );
        return parsedDoc;
    }

    private List<Term> rawTextToTermList(String rawText){
        // in case of empty
        if (StringUtils.isBlank(rawText)){
            return new ArrayList<Term>();
        }

        // parse the rawText
        List<String> terms = new ArrayList<>();

        
        ToAnalysis.parse(rawText).forEach(term -> {
            // do something with term
            terms.add( term.getName() );
        });
        
        List<Term> termList = new ArrayList<>();
        int position = 0;
        // push to the string pool
        for (String term : terms){
            if (!this.stringPool.containsKey(term)){
                this.stringPool.put(term, term);
            }
            term = stringPool.get(term);

            termList.add(new Term(term, position));
            position++;
        }
        
        return termList;
    }

    
}
